import requests
import sqlite3
import pandas as pd
import time
import akshare as ak
import datetime
from bs4 import BeautifulSoup
from comm.tsapis import ts,logger,Tsapi
TimeOut = 5
tb_nm = 'stock_fina_kpi'

def tableCheck(conn):
    curs = conn.cursor()
    sql = "SELECT name num FROM sqlite_master WHERE type='table' AND name='{0}'".format(tb_nm)
    curs.execute(sql)
    isexists = curs.fetchall()
    if len(isexists) > 0:
        curs.close()
    else:
        sql = """CREATE TABLE top10_holder (
         stock_cd TEXT,
         rpt_dt TEXT,
         holder_nm TEXT,
         hold_vol REAL,
         hold_rate REAL,
         chng_typ TEXT,
         chag_vol REAL
         )"""
        curs.execute(sql)
        curs.close()



def deal():
    ts=Tsapi()
    #if 'stocks' not in dir():
    stocks=ts.get_stock_list()
        # 查询当前所有正常上市交易的股票列表
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'};
    # stlist = list(stocks['stock_cd'])
    total = len(stocks)
    today = datetime.datetime.today()
    i = 0
    for js in stocks.to_dict('records'):
        i = i + 1
        code=js['symbol']
        url=f"https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/ZYZBAjaxNew?type=0&code={code}"
        result = requests.session().get(url, headers=headers, timeout=TimeOut)  # requests.get(url
        df=pd.DataFrame(result.json()['data'])
        df.to_sql(tb_nm, ts.conn.get_conn_refresh(), if_exists='append', index=False)
        if(i%200==0):
            logger.info(f"正在获取第{i}个,共：{total} {code}")



if __name__ == '__main__':
    deal()